package test;

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class ChyPainter extends Applet implements KeyListener  {
	
	private int position;
	
	private int oldPosition;
	
	private final int n = 8;
	
	@Override
	public void init() {
		setSize(200, 200);
		position = 5;
		oldPosition = position;
		this.addKeyListener(this);
	}
	
	@Override
	public void paint(Graphics g) {
		Graphics2D g2D = (Graphics2D) g;
		g2D.clearRect(oldPosition, 5, 101, 101);
		g2D.drawRect(position, 5, 100, 100);
		oldPosition = position;
	}
	
	@Override
	public void update(Graphics g) {
		paint(g);
	}
	
	@Override
	public void repaint() {
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		if(KeyEvent.VK_RIGHT == e.getKeyCode()) {
			System.out.println("right");
			position += n;
			update(getGraphics());
		} else if(KeyEvent.VK_LEFT == e.getKeyCode()) {
			System.out.println("left");
			position -= n;
			update(getGraphics());
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
